$(function () {

    initLayerDate("#beginTime", false);

    changeTabs(1, new Date());

    $('body').on('click', '#btnSelcet', function () {
        var beginTime = $("#beginTime").val();
        if (beginTime == '' || beginTime == null || typeof(beginTime) == 'undefined') {
            beginTime = new Date();
        } else {
            beginTime = new Date(beginTime);
        }
        $('#tabs>a').each(function () {
            var _this = $(this);
            if (_this.hasClass('c-info')) {
                var index = $(_this).data("index");
                changeTabs(index, new Date(beginTime));
            }
        });
    }).on('click', '#tabs>a', function () {
        var _this = $(this);
        if (_this.hasClass('c-info')) return;
        _this.siblings().removeClass("c-info");
        _this.addClass("c-info");
        var index = $(_this).data("index");
        var beginTime = $("#beginTime").val();
        if (beginTime == '' || beginTime == null || typeof(beginTime) == 'undefined') {
            beginTime = new Date();
        } else {
            beginTime = new Date(beginTime);
        }
        changeTabs(index, beginTime);
    }).on('click', '.checkPlay', function () {
        var _this = $(this);
        var couId = $(_this).data("id");
        var catalogid = $(_this).data("catalogid");
        sysCheckFn(_this, '', 3, couId + '|' + catalogid);
    });

    function changeTabs(tab, date) {
        var mm = date.getMonth() + 1;
        if (mm <= 9) {
            mm = '0' + mm;
        }
        var yyyy = date.getFullYear() + '-' + mm + '-' + date.getDate();
        $("#beginTime").val(yyyy);
        switch (tab) {
            case 1://全部
                $("#courseTypeKey").val('');
                queryUserLiveSchedule(date);
                break;
            case 2://直播
                $("#courseTypeKey").val('LIVE');
                queryUserLiveSchedule(date);
                break;
            case 3://小班课
                $("#courseTypeKey").val('SMALL');
                queryUserLiveSchedule(date);
                break;

        }
        $("#courseTypeKey").val("")
    }


    function queryUserLiveSchedule(date) {
        var params = {
            "year": date.getFullYear(),
            "month": date.getMonth() + 1,
            "date": date.getDate(),
            "courseTypeKey": $("#courseTypeKey").val()
        };
        params = JSON.stringify(params);
        $.ajax({
            url: baseloaction + '/web/uc/course/play/record/ajax/userLiveSchedule',
            data: params,
            type: 'post',
            dataType: 'text',
            async: false,
            contentType: 'application/json;charset=utf-8',
            success: function (result) {
                $("#live_list_context").html(result)
            }
        });
    }

    function initLayerDate(selector, time) {
        var format = time ? 'YYYY-MM-DD hh:mm:ss' : 'YYYY-MM-DD';
        if ($(selector).length > 0) {
            laydate({
                elem: selector,
                format: format,
                istime: time
            });
        }
    }
});